home *** CD-ROM | disk | FTP | other *** search
- Path: news.delphi.com!usenet
- From: Derek Harmon <stonelight@delphi.com>
- Newsgroups: comp.lang.c
- Subject: Re: New C Programmer Has A Problem
- Date: Tue, 30 Jan 96 14:25:21 -0500
- Organization: Delphi (info@delphi.com email, 800-695-4005 voice)
- Message-ID: <JLNqQp5.stonelight@delphi.com>
- References: <4ehpa3$6kl@nntp.novia.net> <4ek12b$1li@clare.res.com>
- NNTP-Posting-Host: bos1f.delphi.com
- X-To: <danlynes@res.com>
-
- ** Quoting a reply posted by <danlynes@res.com> dated <30-Jan-1996>:
-
- > >In <4ehpa3$6kl@nntp.novia.net>, tsyslo@oasis.novia.net (Tony Syslo) writes:
- :
- > > }
- > > /* We have now opened a file, and are ready to start writing: */
- > > bytes_written=Write(file_handle,name,sizeof(name));
- > > bytes_written=bytes_written+Write(file_handle,age,sizeof(age));
- >
- > You cannot get sizeof( name ), as name is an uninitialize pointer. Please
- > see my reference to malloc above. As I used malloc, we will not use sizeof,
- > as you cannot get a sizeof of a generic pointer...only a struct, union, or
- > dynamically allocated array.
- ^^^^^^^^^^^
- The reason I don't believe malloc() will work out here is because
- sizeof() is a compile-time operation, and therefore cannot be used to
- determine the size of a dynamically allocated array. Everything that
- uses sizeof() in C source code will be converted to an unsigned int
- (type_t) before linking occurs. When name is a static array, sizeof()
- will correctly give its size. When it is dynamically allocated,
- sizeof() will give the size of a pointer.
-
- Example:
- char name[8];
- char *zeffo;
-
- zeffo = (char *)malloc( (12 * sizeof(char)) );
- printf(" array %d\n dynamic %d\n", sizeof(name), sizeof(zeffo));
- free( zeffo );
-
- Output (on a PC compiled in the large memory model):
- array 8
- dynamic 4
-
- -- Stone
- --
- ... Is a computer language without GOTO's totally Wirth-less?
-
-